package com.qf.news.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.news.entity.NewsComment;
import com.qf.news.mapper.NewsCommentMapper;
import com.qf.news.service.NewsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NewsCommentServiceImpl implements NewsCommentService {

    @Autowired
    private NewsCommentMapper newsCommentMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insert(NewsComment newsComment) {
        int i = newsCommentMapper.insert(newsComment);
        if(i!=1){
            System.out.println("【添加评论信息】添加失败！");
            throw new RuntimeException("【添加评论信息】添加失败！");
        }
    }

    @Override
    public PageInfo<NewsComment> findByIdAndPage(Integer page, Integer rows, Long id) {
        //1、分页
        PageHelper.startPage(page, rows);

        //2、查询
        List<NewsComment> commentList=newsCommentMapper.findByNewsId(id);

        //3、封装PageInfo
        PageInfo<NewsComment> pageInfo = new PageInfo<>(commentList);

        //4、返回
        return pageInfo;
    }
}
